package com.zpxt.zpxt.serve;

import com.zpxt.zpxt.dao.ApplicationRepository;
import com.zpxt.zpxt.dao.ResumeRepository;
import com.zpxt.zpxt.dao.TalentRepository;
import com.zpxt.zpxt.entity.Application;
import com.zpxt.zpxt.entity.Resume;
import com.zpxt.zpxt.entity.Talent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @Author:hjx
 * @Description:
 * @Date: Created in 18:51 2018/4/16
 * @Modified By:
 */
@Service("talentServe")
public class TalentServe {
    @Autowired
    ResumeRepository resumeRepository;
    @Autowired
    ApplicationRepository applicationRepository;
    @Autowired
    TalentRepository talentRepository;
    //  简历新增
    public void addResume(Resume resume){
        resumeRepository.save(resume);
    }
    // 简历修改
    public void editResume(Resume resume, Long talentId) throws Exception {
        Optional<Resume> optional = resumeRepository.findById(resume.getId());
        Resume resume1 = optional.get();
        if(resume1.getTalentId()==talentId){
            resumeRepository.save(resume);
        }else{
            throw new Exception("只能修改自己发布的简历！");
        }
    }
    // 简历删除
    public void delResume(Long id,Long talentId) throws Exception {
        Optional<Resume> optional = resumeRepository.findById(id);
        Resume resume =optional.get();
        if(resume.getTalentId()==talentId){
            resumeRepository.delete(resume);
        }else {
            throw new Exception("只能删除自己的简历！");
        }
    }
    // 简历列表查询
    public List<Resume> findResumeList(Long talentId){
        List<Resume> resumes = resumeRepository.findByTalentId(talentId);
        return  resumes;
    }
    // 简历查询
    public Resume findResumeOne(Long id) throws Exception {
        Optional<Resume> optional = resumeRepository.findById(id);
        Resume resume =optional.get();
        return  resume;
    }
    // 申请职位
    public void apply(Long positionId,Long resumeId) throws Exception {
        Resume resume = resumeRepository.findById(resumeId).get();
        List<Map> list = applicationRepository.isApplyed(positionId,resume.getTalentId());
        if(list.size() == 0){
            Application application =new Application();
            application.setPositionId(positionId);
            application.setResumeId(resumeId);
            application.setStatus(Application.ToBeAudited);
            application.setAppTime(new Date());
            applicationRepository.save(application);
        }else {
            throw new Exception("不能重复申请！");
        }
    }
//    申请列表
    public List<Map> appList(Long positionId){
        List<Map> list = applicationRepository.findAppByTalentId(positionId);
        return  list;
    }
//  删除申请
    public void delApp(Long id,Long resumeId) throws Exception {
        Application application = applicationRepository.findById(id).get();
        if(application.getResumeId()==resumeId){
            applicationRepository.delete(application);
        }else {
            throw new Exception("只能删除自己的职位申请！");
        }
    }
    // 查询求职者信息
    public Talent findTalentInfo(Long talentId){
        Talent talent =talentRepository.findById(talentId).get();
        talent.setCode("");
        talent.setPassword("");
        return talent;
    }
}
